package com.sixmac.controller;

import com.google.common.base.Strings;
import com.sixmac.annotation.AuthUser;
import com.sixmac.core.annotation.Validator;
import com.sixmac.core.base.model.Format;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.model.Report;
import com.sixmac.model.User;
import com.sixmac.service.CategoryService;
import com.sixmac.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * Created by admin on 2018/6/9.
 */
@Controller
@ResponseBody
public class ContentController extends ApiController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    ContentService contentService;

    /**
     * @api {post} /api/index  首页
     * @apiName index.index
     * @apiGroup index
     * @apiParam {String}  userId  用户id
     * @apiParam {String} page 当前页 默认为1
     * @apiParam {String} size 每页大小 默认为3
     * @apiSuccess {String} data.page 当前页
     * @apiSuccess {String} data.pageSize 每页大小
     * @apiSuccess {String} data.records 一共有多少数据
     * @apiSuccess {String} data.total 总页数
     * @apiSuccess {String} data.rows.id 文章id
     * @apiSuccess {String} data.rows.img 封面图片
     * @apiSuccess {String} data.rows.title 标题
     * @apiSuccess {String} data.rows.length 视频播放时间
     * @apiSuccess {String} data.rows.url 地址
     * @apiSuccess {String} data.rows.isCollection 自己是否收藏 0没收藏 1收藏
     * @apiSuccess {String} data.rows.content 内容
     * @apiSuccess {String} data.rows.palynum 播放量
     * @apiSuccess {String} data.rows.thumbsnum 点赞量
     * @apiError {Integer} 1
     */
    @RequestMapping("/index")
    public AjaxResult index(String userId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        AjaxResult res = new AjaxResult();

        BladePage<Map> maps = null;
        if (Strings.isNullOrEmpty(userId)) {
            maps = Db.paginate(("select *,0 as 'isCollection' from t_content c  "), null, page, size);
        } else {
            User u = Blade.create(User.class).findById(userId);
            String categoryIds = u.getCategoryIds();
            if (categoryIds == null || StringUtils.isEmpty(categoryIds.trim())) {
                categoryIds = categoryService.getFirstCategoryIds();
            }
            maps = Db.paginate(("select *,(select count(id) from t_collections where userId = " + userId + " and articleId= c.id) as 'isCollection' from t_content c   where c.categoryId in (" + categoryIds + ")"), null, page, size);

        }
        return res.setData(maps);
    }


    /**
     * @api {post} /api/getArticleById  通过id获取文章
     * @apiName index.getArticleById
     * @apiGroup index
     * @apiParam {String}  aid  文章aid
     * @apiParam {String}  userId  用户Id
     * @apiSuccess {String} data.rows.id 文章id
     * @apiSuccess {String} data.rows.img 封面图片
     * @apiSuccess {String} data.rows.title 标题
     * @apiSuccess {String} data.rows.length 视频播放时间
     * @apiSuccess {String} data.rows.url 地址
     * @apiSuccess {String} data.rows.content 内容
     * @apiSuccess {String} data.rows.categoryId 分类id
     * @apiSuccess {String} data.rows.palynum 播放量
     * @apiSuccess {String} data.rows.thumbsnum 点赞量
     * @apiSuccess {String} data.rows.isthumb 自己是否点赞 0 没 1是
     * @apiSuccess {String} data.rows.iscollection 自己是否收藏 0 没 1是
     * @apiError {Integer} 1
     */
    @RequestMapping("/getArticleById")
    @Validator(name = {"aid"}, format = {Format.NOT_NULL})
    public AjaxResult getArticleById(Integer aid, Integer userId) {
        return contentService.getArticleById(aid, userId);
    }

    /**
     * @api {post} /api/getMore  获取更多
     * @apiName index.getMore
     * @apiGroup index
     * @apiParam {String}  categoryId  分类id
     * @apiParam {String} page 当前页 默认为1
     * @apiParam {String} size 每页大小 默认为3
     * @apiSuccess {String} data.page 当前页
     * @apiSuccess {String} data.pageSize 每页大小
     * @apiSuccess {String} data.records 一共有多少数据
     * @apiSuccess {String} data.total 总页数
     * @apiSuccess {String} data.rows.id 文章id
     * @apiSuccess {String} data.rows.img 封面图片
     * @apiSuccess {String} data.rows.title 标题
     * @apiSuccess {String} data.rows.length 视频播放时间
     * @apiSuccess {String} data.rows.url 地址
     * @apiSuccess {String} data.rows.content 内容
     * @apiSuccess {String} data.rows.palynum 播放量
     * @apiSuccess {String} data.rows.thumbsnum 点赞量
     * @apiError {Integer} 1
     */
    @RequestMapping("/getMore")
    @Validator(name = {"categoryId"}, format = {Format.NOT_NULL})
    public AjaxResult getMore(Integer categoryId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        String sql = "select * from t_content where categoryId=" + categoryId + " order by thumbsnum desc";
        BladePage<Map> mapBlade = Db.paginate(sql, null, page, size);
        return new AjaxResult().setData(mapBlade);
    }


    /**
     * @api {post} /api/thumb  点赞/取消
     * @apiName index.thumb
     * @apiGroup index
     * @apiParam {String}  aid  文章aid
     * @apiParam {String}  token  token
     * @apiParam {String}  type  1 点赞 2 取消点赞
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/thumb")
    @Validator(name = {"aid", "type"}, format = {Format.NOT_NULL, Format.NOT_NULL})
    public AjaxResult thumb(Integer aid, User user, Integer type) {
        return contentService.thumb(user.getId(), aid, type);
    }

    /**
     * @api {post} /api/collection  收藏/取消
     * @apiName index.collection
     * @apiGroup index
     * @apiParam {String}  aid  文章aid
     * @apiParam {String}  token  token
     * @apiParam {String}  type  1 点赞 2 取消点赞
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/collection")
    @Validator(name = {"aid", "type"}, format = {Format.NOT_NULL, Format.NOT_NULL})
    public AjaxResult collection(Integer aid, User user, Integer type) {
        return contentService.collection(user.getId(), aid, type);
    }

    /**
     * @api {post} /api/report  举报
     * @apiName index.report
     * @apiGroup index
     * @apiParam {String}  aid  文章aid
     * @apiParam {String}  token  token
     * @apiParam {String}  type  1，抄袭2，反对，依次类推
     * @apiParam {String}  token  token
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/report")
    @Validator(name = {"aid", "type"}, format = {Format.NOT_NULL, Format.NOT_NULL})
    public AjaxResult report(Integer aid, User user, Integer type, String reason) {
        Integer userId = user.getId();
        AjaxResult res = new AjaxResult();
        Report report = new Report();
        report.setArticleId(aid);
        report.setUserId(userId);
        report.setType(type);
        report.setCreateDate(new Date());
        report.setReason(reason);
        boolean b = Blade.create(Report.class).save(report);
        if (b) {
            res.addSuccess("举报成功");
        } else {
            res.addError("举报失败");
        }
        return res;
    }

    /**
     * @api {post} /api/editComment  写评论
     * @apiName index.editComment
     * @apiGroup index
     * @apiParam {Integer}  aid  文章aid
     * @apiParam {String}  content  品论内容
     * @apiParam {String}  token  token
     * @apiParam {Integer}  pid  默认为0,0表示一级评论  ，如果回复评论，传入被评论的id
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/editComment")
    @Validator(name = {"aid", "content"}, format = {Format.NOT_NULL, Format.NOT_NULL})
    public AjaxResult editComment(User user, Integer aid, @RequestParam(defaultValue = "0") Integer pid, String content) {
        return contentService.editComment(user.getId(), aid, pid, content);
    }

    /**
     * @api {post} /api/showComment  显示评论
     * @apiName index.showComment
     * @apiGroup index
     * @apiParam {String}  page  当前页 默认 1
     * @apiParam {String}  size  每页大小 默认 2
     * @apiParam {String}  aid  文章id
     * @apiSuccess {String} lists.lists 表示如果评论回复评论lists不为空
     * @apiSuccess {String} lists.map 评论内容
     * @apiSuccess {String} lists.map.id 评论id
     * @apiSuccess {String}lists.map.articleId 评论文章id
     * @apiSuccess {String} lists.map.content 文章id
     * @apiSuccess {String} lists.map.createDate 评论时间
     * @apiError {Integer} 1
     */
    @RequestMapping("/showComment")
    @Validator(name = {"aid"}, format = {Format.NOT_NULL})
    public AjaxResult showComment(Integer aid, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        return contentService.showComment(aid, page, size);
    }


}
